//
//  UserViewController.m
//  SweetComics
//
//  Created by LeeZhe on 15/1/28.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "UserViewController.h"
#import "ManCell.h"
#import "WeViewController.h"
#import "WelcomeViewController.h"
@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIAlertView *alert;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"用户";
    self.dbHandler = [DataBaseHandler shareInstance];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[[UITableView alloc] init] autorelease];
    [_tableView release];
    
    self.arr = [NSMutableArray arrayWithObjects:@"甜心的自我介绍", @"关于我们", @"欢迎动画", @"版本更新", @"清除缓存", nil];
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {

    }
    
    if (indexPath.row == 1) {
        WeViewController *weVC = [[WeViewController alloc] init];
        [self.navigationController pushViewController:weVC animated:NO];
        [weVC release];
    }
    
    if (indexPath.row == 2) {
        self.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBarHidden = YES;
        WelcomeViewController *welcomeVC = [[WelcomeViewController alloc] init];
        [self.navigationController pushViewController:welcomeVC animated:NO];
        self.hidesBottomBarWhenPushed = NO;
        [welcomeVC release];
    }
    if (indexPath.row == 3) {
        self.alert = [[UIAlertView alloc]initWithTitle:@"当前已是最新版本！" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dismissAlertView:) userInfo:nil repeats:NO];
        
        [_alert show];
        [self.alert release];
        
    }
    if (indexPath.row == 4) {
        
        self.alert = [[UIAlertView alloc]initWithTitle:@"清除缓存" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [_alert show];
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dismissAlertView:) userInfo:nil repeats:NO];
        [_alert release];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        for (ComicList *comic in [self.dbHandler selectCollect]) {
            [self.dbHandler deleteCollect:comic];
        }
        for (ComicList *comic in [self.dbHandler selectAll]) {
            [self.dbHandler deleteComic:comic];
        }
        self.alert = [[UIAlertView alloc]initWithTitle:@"清除缓存成功！" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alert show];
        
        [_alert release];    }
}

- (void)dismissAlertView:(NSTimer*)timer {
    
    NSLog(@"Dismiss alert view");
    
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"我的";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    cell.textLabel.text = [self.arr objectAtIndex:indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)barButtonAction:(UIBarButtonItem *)barButton
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
